package com.qianguo.sender.base;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;


import com.qianguo.sender.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CustomTitle;
import org.androidannotations.annotations.EActivity;

/**
 * 拥有自定义title的基类Activity
 *
 * @author GUXIA318
 * @date 2015-09-21 19:05
 */
@CustomTitle(R.layout.layout_title_bar)
@EActivity
public abstract class BaseTitleActivity extends BaseActivity {

    public Toolbar mToolbar;

    protected abstract String getToolbarTitle();

    protected boolean hasNavigationIcon() {
        return true;
    }

    @AfterViews
    public void initViews() {
        mToolbar = (Toolbar) this.findViewById(R.id.toolbar);
        String title = getToolbarTitle();
        mToolbar.setTitle(TextUtils.isEmpty(title) ? "" : title);
        mToolbar.setTitleTextColor(getResources().getColor(R.color.text_icons_color));
        setSupportActionBar(mToolbar);
        if (hasNavigationIcon()) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationIcon(R.mipmap.ic_arrow_back);
            setToolbarBackClick(mToolbar);
        }

        if (getOnMenuItemClickListener() != null) {
            mToolbar.setOnMenuItemClickListener(getOnMenuItemClickListener());
        }

        initializeUI();
    }

    public void setToolBarTitle(String title) {
        mToolbar.setTitle(title);
    }

    private void setToolbarBackClick(Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
                    getSupportFragmentManager().popBackStack();
                    closeKeyboard(BaseTitleActivity.this);
                } else {
                    finish();
                }
            }
        });
    }

    protected Toolbar.OnMenuItemClickListener getOnMenuItemClickListener() {
        return null;
    }

}
